<template>
  <div class="login-container">
    <el-card class="box-card">
      <div class="title"></div>
      <!-- 表单 -->
      <el-form :model="user" :rules="rules" ref="form">
        <!-- 用户名 -->
        <el-form-item prop="name">
          <el-input v-model="user.name" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="pass">
          <el-input v-model="user.pass" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        <!-- 再次确认密码 -->
        <el-form-item prop="repass">
          <el-input v-model="user.repass" type="password" placeholder="请再次确认密码" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-button type="primary" @click="reg">注册</el-button>
      </el-form>
      <el-link :underline="false" @click="$router.push('/login')">已有账号，去登录</el-link>
    </el-card>
  </div>
</template>

<script>
// 按需导入
import { regAPI } from '@/api/user'

export default {
  name: 'MyReg',
  data() {
    const validatePass2 = (rule, value, callback) => {
      // if (value === '') {
      //   callback(new Error('请再次输入密码'))
      // } else if (value !== this.ruleForm.pass) {
      //   callback(new Error('两次输入密码不一致!'))
      // } else {
      //   callback()
      // }
      if (value === this.user.pass) {
        callback()
      } else {
        callback(new Error('两次输入密码不一致!'))
      }
    }
    return {
      user: {
        name: '',
        pass: '',
        repass: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        pass: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        repass: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
          // 下面是使用自定义的验证规则
          { validator: validatePass2, trigger: 'blur' }
          // 验证两次一致否？
        ]
      }
    }
  },
  // 和data平级，写methods方法
  methods: {
    reg() {
    // form 是 <el-form> 的ref属性值，必须有这个值
      this.$refs.form.validate(async valid => {
      // console.log(valid) // 验证通过valid=true;  验证不通过valid=false
        if (!valid) return // 验证不通过，则return
        // console.log('验证通过，可以提供数据了')
        const data = {
          username: this.user.name,
          password: this.user.pass,
          repassword: this.user.repass
        }
        const { data: res } = await regAPI(data)
        if (res.code === 0) {
        // 注册成功：提示、重置表单、跳转到登录页
          console.log(res)
          this.$message.success(res.message)
          this.$refs.form.resetFields() // 重置表单
          this.$router.push('/login') // 跳转到登录页
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login-container {
    background: url(@/assets/images/login_bg.jpg) no-repeat;
    background-size: cover;
    height: 100%;
    // 标签名就是类名
    .el-card {
      width: 400px;
      // height: 310px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);
      .title {
        height: 40px;
        background: url(@/assets/images/login_title.png)  top center no-repeat;
      }
      .el-button {
        width: 100%;
      }

    }
  }
</style>
